const sequelize = require("./dbConnect");
const md5 = require("md5");

// 定义好表关系
require("./relation");

// 引入模型
const Admin = require("./models/Admin");
const Banner = require("./models/Banner");
require("./models/BlogType");
require("./models/Blog");
require("./models/ProjectModel");
const SettingModel = require("./models/SettingModel");
require("./models/AboutModel");
require("./models/MessageModel");
require("./models/commentModel");

sequelize.sync({ alter: true });

(async () => {
    await sequelize.sync();
    console.log("数据同步完毕");

    // 初始化管理员数据
    const adminCount = await Admin.count();
    if (!adminCount) {
        await Admin.create({
            loginId: "admin",
            loginPwd: md5("1234"),
            name: "超级管理员"
        })
        console.log("初始化管理员数据完毕");
    }

    // 初始化首页标语数据
    const bannerCount = await Banner.count();
    if (!bannerCount) {
        await Banner.bulkCreate([
            {
                "midImg": "/static/images/mid-img-1.jpg",
                "bigImg": "/static/images/big-img-1.jpg",
                "title": "解忧杂货店",
                "description": "将咨询的信件投入杂货店，第二天就能在店后的牛奶箱得到答案",
            },
            {
                "midImg": "/static/images/mid-img-2.jpg",
                "bigImg": "/static/images/big-img-2.jpg",
                "title": "云边有个小卖部",
                "description": "生命是有光的，在我熄灭以前希望能多照亮你一点",
            },
            {
                "midImg": "/static/images/mid-img-3.jpg",
                "bigImg": "/static/images/big-img-3.jpg",
                "title": "人生海海",
                "description": "没有完美的人生，不完美才是人生",
            },
        ])
        console.log("初始化首页标语数据完毕");
    }

    // 初始化全局设置数据
    const settingCount = await SettingModel.count();
    if (!settingCount) {
        await SettingModel.create({
            avatar: "https://lgy-assets.oss-cn-shanghai.aliyuncs.com/my-img/IMG_4233.PNG", 
            siteTitle:"LGY的个人空间",
            github: "https://github.com/v-lgy",
            qq: "2243089982",
            qqQrCode: "https://lgy-assets.oss-cn-shanghai.aliyuncs.com/my-img/qq-QRCode.jpg",
            weixin: "LiGY2282",
            weixinQrCode: "https://lgy-assets.oss-cn-shanghai.aliyuncs.com/my-img/wexin-QRCode.jpg",
            mail: "v.lgy@qq.com",
            icp: "Null",
            githubName: "黑色蒲G英",
            favicon: "https://lgy-assets.oss-cn-shanghai.aliyuncs.com/my-img/favicon.ico",
        })
        console.log("初始化全局设置数据完毕");
    }
})()